package com.rfchina.okr.xianren.base;

import com.jfinal.captcha.CaptchaRender;
import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.rfchina.okr.xianren.util.MyUtil;

public abstract class BaseNoLoginController extends Controller {

	protected void myRenderJson(int code, String msg, Object data) {
		myRenderJsonBean(new ResultBean(code, msg, data));
	}

	protected void myRenderJsonBean(ResultBean bean) {
		// renderJson(bean);
		myRenderJsonObject(bean);

	}

	protected long loginUid() {
		Long uid = getAttr("__uid");
		return uid == null ? 0 : uid;
	}

	protected String getAppid() {
		return getAttr("__appid");
	}

	protected void myRenderJsonObject(Object bean) {
		// renderText(JsonKit.toJson(bean));
		renderJson(bean);
	}

	protected void myRenderJson(Object data) {
		myRenderJsonBean(new ResultBean(200, "ok", data));
	}

	protected void myRenderJsonSuccess(String msg) {
		myRenderJsonSuccess(msg, null);
	}

	protected void myRenderJsonSuccess(String msg, Object bean) {
		myRenderJsonBean(new ResultBean(200, StrKit.isBlank(msg) ? "ok" : msg, bean));
	}

	protected void verification(boolean flag, String msg) {
		MyUtil.verification(flag, msg);
	}

	protected void verification(boolean flag, String msg, int code) {
		MyUtil.verification(flag, msg, code);
	}

	protected void verificationCaptcha(String verify_code) {
		if (!CaptchaRender.validate(this, verify_code)) {
			throw new ErrorRunTimeException(405, "验证码不正确!");
		}
	}

}
